import TRTC from "trtc-js-sdk";

interface optionsConfig {
  sdkAppId: String;
  userId_: String;
  userSig_: String;
  roomId_: Number;
  privateMapKey_?: String;
  mode_: TRTC.ClientConfig.mode | "rtc"; //由live 和 rtc; 在线会议符合需求，直播需要请求上麦，更换身份才能开启视频直播流
  // isJoined_: Boolean;
  // isPublished_: Boolean;
  // isAudioMuted: Boolean;
  // isVideoMuted: Boolean;
  // localStream_: TRTC.LocalStream;
  // remoteStreams_: TRTC.LocalStream[];
}

export default class LiveClient {
  constructor(options: optionsConfig) {
    this.sdkAppId = options.sdkAppId;
    this.userId_ = options.userId_;
    this.roomId_ = options.roomId_;
    this.userSig_ = options.userId_;
    this.privateMapKey_ = options.privateMapKey_;
    this.mode_ = optionsConfig.mode_;

    this.isJoined_ = false; //判断是否已经进入房间

    this.client_ = TRTC.createClient({
      mode: this.mode_, //视频会议
      sdkAppId: this.sdkAppId_,
      userId: this.userId_,
      userSig: this.userSig_
    });
  }

  async join() {
    if (this.isJoined_) {
      console.warn("已经进入房间");
      return;
    }

    try {
      // join the room
      await this.client_.join({
        roomId: parseInt(this.roomId_)
      });
      console.log("join room success");
      this.isJoined_ = true;
    } catch (e) {
      console.log(e);
    }
  }
}
